home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / pseudoDoc / ExampleHeaders / IOEventSource.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  8.1 KB  |  236 lines

  1. /*
  2. Copyright (c) 1998 Apple Computer, Inc.  All rights reserved.
  3. HISTORY
  4.      1998-7-13    Godfrey van der Linden(gvdl)
  5.          Created.
  6.      1998-10-30    Godfrey van der Linden(gvdl)
  7.          Converted to C++
  8. */
  9.  
  10. /* NOTE:  This file is expressly for use with HeaderDoc, for testing purposes.  It is not the current CFIOEventSource.h file! */
  11.  
  12.  
  13. /*! @language embedded-c++ */
  14.  
  15. #ifndef _IOKIT_IOEVENTSOURCE_H
  16. #define _IOKIT_IOEVENTSOURCE_H
  17.  
  18. #include <sys/cdefs.h>
  19.  
  20. #include <libkern/c++/OSObject.h>
  21.  
  22. #include <IOKit/IOLib.h>
  23. #include <IOKit/mach3xxx.h>
  24.  
  25. __BEGIN_DECLS
  26. #include <mach/clock_types.h>
  27. #include <kern/clock.h>
  28. __END_DECLS
  29.  
  30. /* @@@gvdl: Fix ASAP */
  31. /*!
  32.      @defined IOEventSourceAction
  33.      @discussion Back compatibility macro for new class based typing.
  34. */
  35.   #define IOEventSourceAction IOEventSource::Action
  36.  
  37.  
  38. class IOWorkLoop;
  39.  
  40. /*!
  41.      @class IOEventSource : public OSObject
  42.      @abstract Abstract class for all work-loop event sources.
  43.      @discussion The IOEventSource declares the abstract super class that all
  44. event sources must inherit from if an IOWorkLoop is to receive events 
  45. from them.
  46.  
  47. An event source can represent any event that should cause the work-loop of a
  48. device to wake up and perform work.  Two examples of event sources are the
  49. IOInterruptEventSource which delivers interrupt notifications and IOCommandGate
  50. which delivers command requests.
  51.  
  52. A kernel module can always use the work-loop model for serialising access to
  53. anything at all.  The IOEventSource is used for communicating events to the
  54. work-loop, and the chain of event sources should be used to walk the possible
  55. event sources and demultipex them.  Note a particular instance of an event
  56. source may only be a member of 1 linked list chain.  If you need to move it
  57. between chains than make sure it is removed from the original chain before
  58. attempting to move it.
  59.  
  60. The IOEventSource makes no attempt to maintain the consitency of it's internal
  61. data across multi-threading.  It is assumed that the user of these basic tools
  62. will protect the data that these objects represent in some sort of device wide
  63. instance lock.  For example the IOWorkLoop maintains the event chain by handing
  64. off change request to its own thread and thus single threading access to its
  65. state.
  66.  
  67. All subclasses of the IOEventSource are expected to implement the 
  68. checkForWork()
  69. member function.
  70.  
  71. checkForWork() is the key method in this class.  It is called by some work-loop
  72. when convienient and is expected to evaluate it's internal state and determine
  73. if an event has occured since the last call.  In the case of an event having
  74. occurred then the instance defined target(owner)/action will be called.  The
  75. action is stored as an ordinary C function pointer but the first parameter is
  76. always the owner.  This means that a C++ member function can be used as an
  77. action function though this depends on the ABI.
  78.  
  79. Although the eventChainNext variable contains a reference to the next event
  80. source in the chain this reference is not retained.  The list 'owner' i.e. the
  81. client that creates the event, not the work-loop, is expected to retain the
  82. source.
  83. */
  84. class IOEventSource : public OSObject
  85. {
  86.      OSDeclareAbstractStructors(IOEventSource)
  87.      friend class IOWorkLoop;
  88.  
  89. public:
  90. /*!
  91.      @typedef Action
  92.      @discussion Placeholder type for C++ function overloading discrimination.
  93. As the all event sources require an action and it has to be stored somewhere
  94. and be of some type, this is that type.
  95.      @param owner
  96.          Target of the function, can be used as a refcon.  The owner is set
  97. during initialisation.     Note if a C++ function was specified this parameter
  98. is implicitly the first paramter in the target member function's 
  99. parameter list.
  100. */
  101.      typedef void (*Action)(OSObject *owner, ...);
  102.  
  103. protected:
  104. /*! @var eventChainNext
  105.          The next event source in the event chain. nil at end of chain. */
  106.      IOEventSource *eventChainNext;
  107.  
  108. /*! @var owner The owner object called when an event has been delivered. */
  109.      OSObject *owner;
  110.  
  111. /*! @var action
  112.          The action method called when an event has been delivered */
  113.      Action action;
  114.  
  115. /*! @var enabled
  116.          Is this event source enabled to deliver requests to the work-loop. */
  117.      bool enabled;
  118.  
  119. /*! @var workLoop What is the work-loop for this event source. */
  120.      IOWorkLoop *workLoop;
  121.  
  122. /*!
  123.     @function getID
  124.     HeaderDoc test of multiline inline function definition.
  125. */
  126. int getID() {
  127.     return id;
  128. }
  129.  
  130. /*! @function init
  131.      @abstract Primary initialiser for the IOEventSource class.
  132.      @param owner
  133.          Owner of this instance of an event source.  Used as the first parameter
  134. of the action callout.  Owner will generally be an OSObject it doesn't have to
  135. be as no member functions will be called directly in it.  It can just be a
  136. refcon for a client routine.
  137.      @param action
  138.          Pointer to C call out function.  Action is a pointer to a C function
  139. that gets called when this event source has outstanding work.  It will usually
  140. be called by the checkForWork member function.  The first parameter of the
  141. action call out will always be the owner, this allows C++ member functions to
  142. be used as actions.  Defaults to 0.
  143.      @result true if the inherited classes and this instance initialise
  144. successfully.
  145. */
  146.      virtual bool init(OSObject *owner, IOEventSource::Action action = 0);
  147.  
  148. /*! @function checkForWork
  149.      @abstract Pure Virtual member function used by IOWorkLoop for work
  150. scheduling.
  151.      @discussion This function will be called to request a subclass to check
  152. it's internal state for any work to do and then to call out the owner/action.
  153.      @param moreP
  154.          Pointer to the more-work output variable.  Set to true if this function
  155. needs to be called again before all its outstanding events have been processed.
  156.      @param wakeupTimeP
  157.          Pointer to no-later wake up time output variable.  Return variable to
  158. indicate when this object wishes to be called again; used for timeouts.
  159. Note the checkForWork method may be called before the timeout fires. 
  160. Defaults to MACH_TIMESPEC_ZERO which is interpreted as no timeout 
  161. requested.
  162. */
  163.      virtual void checkForWork(bool *moreP, mach_timespec_t *wakeupTimeP) = 0;
  164.  
  165. /*! @function setWorkLoop
  166.      @abstract Set'ter for workLoop variable.
  167.      @param workLoop
  168.          Target work-loop of this event source instance.  A subclass of
  169. IOWorkLoop that at least reacts to signalWorkAvailable() and onThread 
  170. functions.
  171. */
  172.      virtual void setWorkLoop(IOWorkLoop *workLoop);
  173.  
  174. /*! @function setNext
  175.      @abstract Set'ter for eventChainNext variable.
  176.      @param next
  177.          Pointer to another IOEventSource instance.
  178. */
  179.      virtual void setNext(IOEventSource *next);
  180.  
  181. /*! @function getNext
  182.      @abstract Get'ter for eventChainNext variable.
  183.      @result value of eventChainNext.
  184. */
  185.      virtual IOEventSource *getNext() const;
  186.  
  187. public:
  188. /*! @function setAction
  189.      @abstract Set'ter for action variable.
  190.      @param action
  191.          Pointer to a C function of type IOEventSource::Action.
  192. */
  193.      virtual void setAction(IOEventSource::Action action);
  194.  
  195. /*! @function getAction
  196.      @abstract Get'ter for action variable.
  197.      @result value of action.
  198. */
  199.      virtual IOEventSource::Action getAction() const;
  200.  
  201. /*! @function enable
  202.      @abstract Enable event source.
  203.      @discussion A subclass implementation is expected to respect the enabled
  204. state when checkForWork is called.  Calling this function will cause the
  205. work-loop to be signalled so that a checkForWork is performed.
  206. */
  207.      virtual void enable();
  208.  
  209. /*! @function disable
  210.      @abstract Disable event source.
  211.      @discussion A subclass implementation is expected to respect the enabled
  212. state when checkForWork is called.
  213. */
  214.      virtual void disable();
  215.  
  216. /*! @function isEnabled
  217.      @abstract Get'ter for enable variable.
  218.      @result true if enabled.
  219. */
  220.      virtual bool isEnabled() const;
  221.  
  222. /*! @function getWorkLoop
  223.      @abstract Get'ter for workLoop variable.
  224.      @result value of workLoop.
  225. */
  226.      virtual IOWorkLoop *getWorkLoop() const;
  227.  
  228. /*! @function onThread
  229.      @abstract Convenience function for workLoop->onThread.
  230.      @result true if called on the work-loop thread.
  231. */
  232.      virtual bool onThread() const;
  233. };
  234.  
  235. #endif /* !_IOKIT_IOEVENTSOURCE_H */
  236.